package com.foreverwin.mes.lims.service.impl;

import com.foreverwin.mes.common.constants.HandleBOConstants;
import com.foreverwin.mes.core.base.FrontPage;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.lims.mapper.InspecArrayItemMapper;
import com.foreverwin.mes.lims.model.InspecArray;
import com.foreverwin.mes.lims.mapper.InspecArrayMapper;
import com.foreverwin.mes.lims.model.InspecArrayItem;
import com.foreverwin.mes.lims.service.InspecArrayItemService;
import com.foreverwin.mes.lims.service.InspecArrayService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Levy
 * @since 2020-02-17
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InspecArrayServiceImpl extends ServiceImpl<InspecArrayMapper, InspecArray> implements InspecArrayService {


    @Autowired
    private InspecArrayMapper inspecArrayMapper;
    @Autowired
    private InspecArrayItemMapper inspecArrayItemMapper;
    @Autowired
    private InspecArrayItemService inspecArrayItemService;

    @Override
    public Page<InspecArray> selectPage(FrontPage<InspecArray> frontPage, InspecArray inspecArray) {
        EntityWrapper<InspecArray> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(inspecArray);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<InspecArray> selectList(String site) {
       Map paramMap = new HashMap();
       paramMap.put( "SITE" , site );

        return inspecArrayMapper.selectByMap( paramMap );
    }

    @Override
    public void doSaveData(InspecArray inspecArray, List<Map> inspectionArrayList) {
        //now Date
        Date nowTime = new Date();
        String arrayBo = null;

        //检验项目是否存在
         InspecArray queryModel = new InspecArray();
         queryModel.setCheckArray( inspecArray.getCheckArray() );
         queryModel.setSite( inspecArray.getSite() );
         queryModel = inspecArrayMapper.selectOne( queryModel );

         if ( queryModel != null ){

             queryModel.setUpdatedDateTime( nowTime );
             queryModel.setUpdatedUser( inspecArray.getCreatedUser() );
             inspecArrayMapper.updateAllColumnById( queryModel );

             //删除检验项目下明细
             arrayBo = queryModel.getHandle();
             inspecArrayItemService.deleteArrayItemByArrayBo( arrayBo );

             insertArrayItem( inspectionArrayList , arrayBo );

         }else {
             //检验项目新增
             InspecArray addModel = new InspecArray();
             arrayBo = HandleBOConstants.INSPEC_ARRAY_BO + ":" + inspecArray.getSite() + "," + inspecArray.getCheckArray();
             addModel.setHandle( arrayBo );
             addModel.setSite( inspecArray.getSite() );
             addModel.setCheckArray( inspecArray.getCheckArray() );
             addModel.setArrayDesc( inspecArray.getArrayDesc() );
             addModel.setCreatedDateTime( nowTime );
             addModel.setCreatedUser( inspecArray.getCreatedUser() );
             inspecArrayMapper.insert( addModel );

             if ( !inspectionArrayList.isEmpty() ){

                 insertArrayItem( inspectionArrayList , arrayBo );
             }

         }
    }

    private void insertArrayItem( List<Map> inspectionArrayList ,String arrayBo){
        for ( Map map : inspectionArrayList ){
            String itemDesc = (String ) map.get("itemDesc");
            String arrayItem = (String ) map.get("arrayItem");
            String judgStand = (String ) map.get("judgStand");
            if (StringUtils.isBlank( arrayItem )){
                throw BusinessException.build("参数编号不能为空");
            }
            if (StringUtils.isBlank( itemDesc )){
                throw BusinessException.build("参数名称不能为空");
            }
            if (StringUtils.isBlank( judgStand )){
                throw BusinessException.build("自动判定标准不能为空");
            }

            InspecArrayItem inspecArrayItem = new InspecArrayItem();
            String handle = HandleBOConstants.INSPEC_ARRAY_ITEM_BO + ":" + arrayBo + "," + (String ) map.get("arrayItem");
            inspecArrayItem.setArrayBo( arrayBo );
            inspecArrayItem.setItemDesc( (String ) map.get("itemDesc") );
            inspecArrayItem.setArrayItem( (String ) map.get("arrayItem") );
            inspecArrayItem.setJudgStand( (String ) map.get("judgStand") );
            inspecArrayItem.setHandle( handle );
            inspecArrayItemMapper.insert( inspecArrayItem );
        }
    }

    @Override
    public InspecArray getInspecArray(String site, String array) {
        InspecArray inspecArray = new InspecArray();
        inspecArray.setCheckArray( array );
        inspecArray.setSite( site );
        inspecArray = inspecArrayMapper.selectOne( inspecArray );

        return inspecArray;
    }
}